using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using equalidator;
using NUnit.Framework;
using wpffc.contracts.domaindata;
using wpffc.flashcardbox.domaindata;

namespace wpffc.flashcardbox.tests
{
    [TestFixture]
    public class test_Fill_batch_from_compartment
    {
        [Test]
        public void Batch_needs_no_refill()
        {
            var box = new FlashCardBox(0, 1, new int[0]);
            box.Batch.Enqueue(new BatchCard());

            var sut = new Fill_batch_from_compartment();
            IFlashCardBox fullBox = null;
            sut.Full += _ => fullBox = _;

            sut.Process(new Tuple<IFlashCardBox, int>(box, 0));

            Assert.AreSame(box, fullBox);
        }


        [Test]
        public void Fill_from_compartment_if_needed()
        {
            var box = new FlashCardBox(0, 2, new[]{1});
            box.Compartments[0].Enqueue(new Card{Question="q1"});
            box.Compartments[0].Enqueue(new Card{Question="q2"});

            var sut = new Fill_batch_from_compartment();
            IFlashCardBox fullBox = null;
            sut.Full += _ => fullBox = _;

            sut.Process(new Tuple<IFlashCardBox, int>(box, 0));

            Assert.AreEqual(BatchStatus.Full, fullBox.Batch.Status);
            Assert.AreEqual("q1", fullBox.Batch.Dequeue().Card.Question);
            Assert.AreEqual("q2", fullBox.Batch.Dequeue().Card.Question);
        }


        [Test]
        public void Cannot_fully_fill_from_compartment()
        {
            var box = new FlashCardBox(0, 2, new[] { 1 });
            box.Compartments[0].Enqueue(new Card { Question = "q1" });

            var sut = new Fill_batch_from_compartment();
            IFlashCardBox continueBox = null;
            sut.ContinueFilling += _ => continueBox = _;

            sut.Process(new Tuple<IFlashCardBox, int>(box, 0));

            Assert.AreEqual(BatchStatus.Filled, continueBox.Batch.Status);
            Assert.AreEqual("q1", continueBox.Batch.Dequeue().Card.Question);
            Assert.AreEqual(CompartmentStatus.Empty, continueBox.Compartments[0].Status);
        }


        [Test]
        public void Map_card_to_batchcard()
        {
            var c = new Card {Question = "q"};
            var bc = Fill_batch_from_compartment.MapCardToBatchCard(1, c);

            Equalidator.AreEqual(new BatchCard {CompartmentIndex = 1, Card = c}, bc);
        }


        [Test]
        public void Check_if_batch_needs_refill()
        {
            var b = new Batch(1, 3);
            Assert.IsTrue(Fill_batch_from_compartment.BatchNeedsRefill(b));

            b.Enqueue(new BatchCard());
            Assert.IsTrue(Fill_batch_from_compartment.BatchNeedsRefill(b));

            b.Enqueue(new BatchCard());
            Assert.IsFalse(Fill_batch_from_compartment.BatchNeedsRefill(b));
        }
    }
}